package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/25 9:18
 */
@RequestMapping("/admin/topic")
@RestController
@CrossOrigin
public class TopicController {

    @Autowired
    private DtsTopicService dtsTopicService;

    /**
     * 推广专题显示
     * @param title
     * @param sort
     * @param order
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions(value = "admin:topic:list")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "查询")//通过这个注解知道这是什么方法是干什么的？
    @GetMapping("/list")
    public Object list(String title, String subtitle, String sort, String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit){
        //分页对象
        IPage<DtsTopic> iPage = dtsTopicService.findListByQuery(title,subtitle,sort,order,page,limit);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * /create
     */
    @RequiresPermissions(value = "admin:topic:create")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsTopic dtsTopic){
        dtsTopic = dtsTopicService.create(dtsTopic);
        return ResponseUtil.ok(dtsTopic);

    }

    /**
     * update
     */
    @RequiresPermissions(value = "admin:topic:update")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsTopic dtsTopic){
        dtsTopic = dtsTopicService.update(dtsTopic);
        return ResponseUtil.ok(dtsTopic);

    }

    /**
     * delete
     */
    @RequiresPermissions(value = "admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsTopic dtsTopic){
        dtsTopic = dtsTopicService.delete(dtsTopic);
        return ResponseUtil.ok(dtsTopic);

    }
}
